package com.newsystem.service.impl;

import com.newsystem.MyUtils.CommonResult;
import com.newsystem.MyUtils.MyUUIDUtils;
import com.newsystem.mapper.UserMapper;
import com.newsystem.pojo.Medium;
import com.newsystem.mapper.MediumMapper;
import com.newsystem.pojo.User;
import com.newsystem.pojo.vo.MediumInfo;
import com.newsystem.service.IMediumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 媒体号 服务实现类
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@Service
public class MediumService extends ServiceImpl<MediumMapper, Medium> implements IMediumService {

    @Autowired
    private MediumMapper mediumMapper;
    @Autowired
    private UserMapper userMapper;

    //媒体号注册
    public CommonResult<Object> insertOneMedium(User user,Medium medium){
        List<User> users = userMapper.selectByAccount(user.getAccount());
        if(users.size() > 0){
            return CommonResult.failed("此账号已经存在");
        }
        else {
            user.setId(MyUUIDUtils.getUUID());
            user.setStatue(1);
            if(userMapper.insert(user)>0){
                medium.setUserId(user.getId());
                medium.setId(MyUUIDUtils.getUUID());
                if(mediumMapper.insert(medium)>0){
                    return CommonResult.success("注册成功");
                }
                else {
                    userMapper.deleteById(user);
                }
            }
            return CommonResult.failed("注册失败");
        }
    }
    //根据媒体号id查询该媒体号信息
    public CommonResult<Object> selectMediumInfoByMediumId(String userId){
        MediumInfo mediumInfo = mediumMapper.selectByMediumId(userId);
        if (mediumInfo==null){
            return CommonResult.failed("获取失败");
        }
        if(mediumInfo.getUser().getStatue()!=1){
            return CommonResult.failed("该账号已经注销");
        }
        return CommonResult.success(mediumInfo);


    }

}
